package dungnt.vpoint.mathtest.util;

import java.util.ArrayList;
import java.util.Random;

import dungnt.vpoint.mathtest.util.Operand;

public class Operate {
	private int firstNumber;
	private int secondNumber;
	public  int operator;
	private int level;
	private int correctAnswers;
	private int wrongAnswers;
	private boolean answer;
	private ArrayList<Operand> generatedOperands;
	
	public Operate() {
		this.firstNumber = 0;
		this.secondNumber = 0;
		this.operator = 0;
		this.level = 1;
		this.correctAnswers = 0;
		this.wrongAnswers = 0;
		this.answer = false;
		this.generatedOperands = new ArrayList<Operand>();
	}

	
	public int getFirstNumber() {
		return firstNumber;
	}


	public void setFirstNumber(int firstNumber) {
		this.firstNumber = firstNumber;
	}


	public int getSecondNumber() {
		return secondNumber;
	}


	public void setSecondNumber(int secondNumber) {
		this.secondNumber = secondNumber;
	}

	
	public int getOperator() {
		return operator;
	}


	public void setOperator(int operator) {
		this.operator = operator;
	}


	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}
	
	public void generateOperand() {
		Random rd = new Random();
		answer = false;
		//Generate two operands
		//If operator is '/', check divisibility
		if (operator==3) {
			firstNumber = rd.nextInt(level*10)+1+((level-1)*10);
			secondNumber = rd.nextInt((level-1)*10+1)+2;
			while (firstNumber%secondNumber!=0)
				generateOperand();
			return;
		}
		//If operator is '*'
		if (operator==2) {
			firstNumber = rd.nextInt(level*10)+1+((level-1)*10);
			secondNumber = rd.nextInt((level-1)*10+1)+2;
			return;
		}
		// else
		firstNumber = rd.nextInt(level*10)+1+((level-1)*10);
		secondNumber = rd.nextInt(level*10)+1+((level-1)*10);
		generatedOperands.add(new Operand(firstNumber, secondNumber));
		
	}
	
	public boolean checkDuplicate(int a, int b) {
		Operand temp = new Operand(a, b);
		while (generatedOperands.contains(temp)) {
			return true;
		}
		return false;
	}
	public void generateOperator() {
	    //generate random operator
		Random rd = new Random();
		operator = rd.nextInt(4);
	}
	
	public void levelUp() {
		//Level up if number of correct answers equal 3
		if (correctAnswers==3) {
			level++;
			correctAnswers = 0;
		}
	}
	
	public void levelDown(){
		//Level down if number of correct answers equal 2
		if ((level>=2) && (wrongAnswers==2)) {
				level--;
				wrongAnswers = 0;
		} 
	}
	
	public boolean checkAnswer(int result) {
		//check answer beyond operator
		switch (operator) {
		case 0:
			answer = ((firstNumber+secondNumber)==result);
			break;
		case 1:
			answer = ((firstNumber-secondNumber)==result);
			break;
		case 2:
			answer = ((firstNumber*secondNumber)==result);
			break;
		case 3:
			answer = ((firstNumber/secondNumber) ==result);
			break;
		}
		
		if (answer) {
			correctAnswers++;
			wrongAnswers = 0;
			levelUp();
		} else {
			wrongAnswers++;
			correctAnswers = 0;
			levelDown();
		}
		
		return answer;
	}
}
